package com.zr.planclass.service.impl;

import com.zr.share.config.ErrorMassage;
import com.zr.planclass.mapper.PlanClassMapper;
import com.zr.planclass.model.pojo.PlanClass;
import com.zr.planclass.model.pojo.PlanClassAddVo;
import com.zr.planclass.model.pojo.PlanClassExample;
import com.zr.planclass.model.pojo.PlanClassUpdateVo;
import com.zr.planclass.service.PlanClassService;
import com.zr.utils.page.PageParameterUtil;
import com.zr.utils.page.PageUtil;
import com.zr.utils.result.ResultBuilderUtil;
import com.zr.utils.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName PlanClassServiceImpl
 * @Description  计划大类实现
 * @Author 马家奎
 * @Date 2019/11/7 20:15
 */
@Service
public class PlanClassServiceImpl implements PlanClassService {
    @Autowired
    private PlanClassMapper planClassMapper;
    @Autowired
    private PlanClassExample planClassExample;
    @Autowired
    private PageUtil<PlanClass> page;
    @Autowired
    private ResultUtil resultUtil;
    @Autowired
    private PlanClass planClass;
    //分页＋多条件查询
    @Override
    public PageUtil<PlanClass> queryPage(PageParameterUtil pageParameterUtil) {

        page.setPageIndex(pageParameterUtil.getPageIndex());
        page.setTotalNumber(planClassMapper.getCount(pageParameterUtil));
        page.setPageSize(pageParameterUtil.getPageSize());
        page.setDataList(planClassMapper.getCurrentPageData(pageParameterUtil));

        return page;
    }
    //根据id查询
    @Override
    public PlanClass queryById(Integer id) {

        return planClassMapper.selectByPrimaryKey(id);
    }
    @Transactional
    @Override
    public ResultUtil addPlanClass(PlanClassAddVo planClassAddVo) {
        //验证编码不能重复
        ResultUtil validateCode = validateCode(planClassAddVo.getCode());
        if (!validateCode.getSuccess()) {
            return validateCode;
        }
        //验证名称不能重复
        PlanClass planClass = planClassMapper.queryPlanClassName(planClassAddVo.getName());
        if (planClass!=null){
            return ResultBuilderUtil.error(ErrorMassage.NAME_NOT_REPETITION);
        }
        //从session中获取创建人信息和时间及修改人信息和时间

        BeanUtils.copyProperties(planClassAddVo, this.planClass);
        Date nowTime = new Date();
        this.planClass.setCreatename("马家奎");
        this.planClass.setCreatetime(nowTime);
        this.planClass.setUpdatename("马家奎");
        this.planClass.setUpdatetime(nowTime);

        //添加
        planClassMapper.insert(this.planClass);
        return ResultBuilderUtil.success(null);
    }
    //添加

    //验证编号不能重复
    private ResultUtil validateCode(String  code) {
        int countPlanClassCode =planClassMapper.queryPlanClassCode(code);
        if (countPlanClassCode>0){
            return ResultBuilderUtil.error(ErrorMassage.CODE_NOT_REPETITION);
        }else{

            return ResultBuilderUtil.success(null);
        }

    }
    //验证名称不能重复
    public ResultUtil validateName(PlanClassUpdateVo planClassUpdateVo) {
        PlanClass planClass = planClassMapper.queryPlanClassName(planClassUpdateVo.getName());
        if ( planClass.getId().equals(planClassUpdateVo.getId()) ){
            return ResultBuilderUtil.error(ErrorMassage.NAME_NOT_REPETITION);
        }else {

            return ResultBuilderUtil.success(null);
        }
    }

    //修改
    @Transactional
    @Override
    public ResultUtil updatePlanClass(PlanClassUpdateVo planClassUpdateVo) {
        //判断该计划大类是否存在
        PlanClass planClass = planClassMapper.selectByPrimaryKey(planClassUpdateVo.getId());
        if (planClass == null) {
            return ResultBuilderUtil.error(ErrorMassage.UPDATE_NOT_EXIST);

        }

        //判断该计划大类状态是否禁用
        if (planClass.getStatus()) {
            return ResultBuilderUtil.error(ErrorMassage.UPDATE_STATUS_MUST_CLOSE);
        }
        //判断计划大类名称是否重复
        PlanClass planClas = planClassMapper.queryPlanClassName(planClassUpdateVo.getName());
        if (!planClas.getId().equals(planClassUpdateVo.getId())) {
            return ResultBuilderUtil.error(ErrorMassage.NAME_NOT_REPETITION);
        }
        //从session中获取创建人信息和时间及修改人信息和时间
        BeanUtils.copyProperties(planClassUpdateVo,planClass);
        Date nowTime = new Date();
        planClass.setUpdatename("马家奎");
        planClass.setUpdatetime(nowTime);
        //修改
        int i = planClassMapper.updatePlanClass(planClassUpdateVo);
        if (i ==0) {
            return ResultBuilderUtil.error(ErrorMassage.PLEASE_FLUSH);
        }

        return ResultBuilderUtil.success(null);

    }
    //修改状态
    @Transactional
    @Override
    public ResultUtil planClassStatus(PlanClass planClass) {
        planClassMapper.planClassStatus(planClass);

        return ResultBuilderUtil.success(null);
    }
}
